package com.jinyi.wwdeng.ui.config.act

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.bingoogolapple.baseadapter.BGAOnItemChildClickListener
import com.jinyi.base.base.BaseActivity
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.ActConfigBinding
import com.jinyi.wwdeng.ui.config.adapter.ConfigTabAdapter
import com.jinyi.wwdeng.ui.config.fragment.AccountInfoFragment
import com.jinyi.wwdeng.ui.config.fragment.ConfirmerFragment
import com.jinyi.wwdeng.ui.config.fragment.FaceRecognitionFragment
import com.jinyi.wwdeng.ui.config.fragment.IdentityFragment
import com.jinyi.wwdeng.ui.config.fragment.PersonalInfoFragment
import com.jinyi.wwdeng.ui.permission.PermissionUtils
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/18 10:43
 * @Annotation：资料配置页面
 */
class ConfigAct: BaseActivity() {

    lateinit var binding: ActConfigBinding

    private val mTabTitleList = arrayListOf(R.string.personal_info, R.string.work_info,R.string.contact_info,R.string.account_info,R.string.confirmer_le_compte,R.string.identity_verification,R.string.face_recognition)
    private var mTabAdapter: ConfigTabAdapter? = null
    override fun getRootView(): View {
        binding = ActConfigBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        setTitleBarVisibility(View.VISIBLE)
        setRightBtnRes(R.mipmap.pic_kefu_black_icon)

        initRecyclerView()
    }

    override fun initListener() {

    }

    override fun initData() {
        //tab
        val tabList = arrayListOf<JSONObject>()
        for (i in 0 until this.mTabTitleList.size) {
            val tabObj = JSONObject()
            tabObj.put("tabId", i)
            tabObj.put("tabTitle", this.mTabTitleList[i])
            tabObj.put("isSelect", false)
            tabList.add(tabObj)
        }
        this.mTabAdapter!!.data = tabList
        setSelectConfigTab(0)
    }

    /* 权限请求监听 */
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtils.onRequestPermissionsResult(this,requestCode, permissions,grantResults)
    }

    /**
     * 初始化Tab列表
     */
    private fun initRecyclerView() {
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = RecyclerView.HORIZONTAL
        binding.recyclerView.layoutManager = layoutManager
        mTabAdapter = ConfigTabAdapter(binding.recyclerView)
        binding.recyclerView.adapter = mTabAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        mTabAdapter!!.setOnItemChildClickListener(BGAOnItemChildClickListener { _, childView, position ->
            if (childView.id == R.id.itemLayout) {//切换item
                setSelectConfigTab(position)
            }
        })
    }

    /**
     * 切换选中
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun setSelectConfigTab(index: Int){
        for (i in 0 until mTabAdapter!!.itemCount) {
            val item = mTabAdapter!!.getItem(i)
            if(i == index){
                item.put("isSelect", true)
                setTextTitle(getString(item.optInt("tabTitle")))
            } else {
                item.put("isSelect", false)
            }
        }
        mTabAdapter!!.notifyDataSetChanged()
        checkFragmentInfo(index)
    }

    /**
     * 切换显示的内容
     */
    private fun checkFragmentInfo(index: Int){
        when(index){
            0 -> {//个人信息
                replaceContainerFragement(R.id.contentLayout,PersonalInfoFragment())
            }
            3 -> {//账户信息
                replaceContainerFragement(R.id.contentLayout,AccountInfoFragment())
            }
            4 -> {//确认信息
                replaceContainerFragement(R.id.contentLayout,ConfirmerFragment())
            }
            5 -> {//身份验证
                replaceContainerFragement(R.id.contentLayout, IdentityFragment())
            }
            6 -> {//人脸识别
                replaceContainerFragement(R.id.contentLayout, FaceRecognitionFragment())
            }
        }
    }
}